<template>
	<view>
		<view v-if="addressLists.length" class="mt10 plr14">
			<view class="bgWhite   ptb15 mb10 radius10" @click="chooseAddress(item)"
				v-for="(item,index) in addressLists" :key="index">
				<view class="bdbe plr15 pb10">
					<view class="align-center">
						<view class="gray3 bold mr10"><text>{{item.real_name}}</text></view>
						<view class="gray3 bold mr10"><text>{{item.phone}}</text></view>
					</view>
					<view class="align-center pt10">
						<view class="ft13" style="width: 634rpx;">
							<text>{{item.province}}{{item.city}}{{item.district}}{{item.detail}}</text>
						</view>
					</view>
				</view>
				<view class="plr15 justify-between align-center pt15">
					<view class="align-center" @click.stop="setdefault(item)">
						<image
							:src="item.is_default==1?'../../static/images/mine/icon_yuan_y.png':'../../static/images/mine/icon_yuan_n.png'"
							mode="aspectFill" style="width: 24rpx;height: 24rpx;"></image>
						<view class="ml5 ft12 gray9"><text>{{$t('mine').设为默认地址}}</text></view>
					</view>
					<view class="align-center mr12">
						<view class="align-center"
							@click.stop="navTo(`/pages/address/addressDetail?address_id=${item.address_id}&isadd=1`)">
							<image src="../../static/images/mine/exit.png" mode="aspectFill"
								style="width: 20rpx;height: 20rpx;"></image>
							<view class="ml5 ft12 gray9"><text>{{$t('mine').编辑}}</text></view>
						</view>
						<view class="align-center ml12" @click.stop="openModel(index)">
							<image src="../../static/images/mine/dele.png" mode="aspectFill"
								style="width: 20rpx;height: 22rpx;"></image>
							<view class="ml5 ft12 gray9"><text>{{$t('mine').删除}}</text></view>
						</view>
					</view>
				</view>
				<!--  @click.stop="navTo(`/pages/address/addressDetail?address_id=${item.address_id}`)" -->
			</view>
			<view></view>
		</view>
		<view v-else style="padding-top: 168rpx;">
			<view class="justify-center">
				<image src="../../static/images/address/wudizhi.png" mode="aspectFill"
					style="width: 446rpx;height: 356rpx;">
				</image>
			</view>
			<view class="ft12 gray9 tc"><text>{{$t('mine').暂无地址快去添加吧}}</text></view>
		</view>
		<view class="posFixed w100" style="bottom: 20rpx;">
			<mix-button ref="confirmBtn" :text="$t('mine').新增收货地址"
				@onConfirm="navTo('/pages/address/addressDetail')"></mix-button>
		</view>
		<!-- 底部弹窗   是否是指默认密码 -->
		<view class="tc">
			<u-modal :showCancelButton="true" :confirmText="$t('common').确认" confirmColor="#F44A42"
				cancelColor="#333333" :cancelText="$t('common').取消" width="600rpx" :show="show"
				:content="$t('mine').确认删除当前收货地址吗" :closeOnClickOverlay="closeOnClickOverlay" @cancel="cancel()"
				@close="cancel()" @confirm="deleteAddress()"></u-modal>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				addressLists: [],
				other: '',
				show: false,
				closeOnClickOverlay: true,
				seladdress: -1,
			}
		},
		onShow() {
			this.addresslistAPI()
		},
		onLoad(options) {
			this.$store.dispatch('getuserInfo')
			this.other = options.other
			uni.setNavigationBarTitle({
				title: this.$t('mine').收货地址
			})
		},
		methods: {
			async addresslistAPI() {
				const res = await this.$api.addresslist()
				if (res && res.code == 1) {
					this.addressLists = res.data
				}
			},
			//选择地址
			chooseAddress(item) {
				if (this.other == 1) {
					let pages = getCurrentPages() // 获取当前页面栈的实例，以数组形式按栈的顺序给出，第一个元素为首页，最后一个元素为当前页面。
					if (pages.length > 1) {
						// uni.navigateBack()
						let prevPage = pages[pages.length - 2] //上一页页面实例
						uni.navigateBack({
							delta: 1,
							success: () => {
								prevPage.$vm.chooseAddress(item) // 给上一页绑定方法otherFun,传参地址id
							}
						})
					} else {
						uni.showToast({
							title: '出现错误，请刷新后重试',
							icon: 'error'
						})
						uni.reLaunch({
							url: `/pages/address/addressDetail?address_id=${item.address_id}`
						}, 3000)
					}
				} else {

				}
			},
			cancel() {
				this.show = false
			},
			openModel(index) {
				this.show = true
				this.seladdress = index
			},
			// 删除收货地址
			async deleteAddress() {
				// uni.showModal({
				// 	content: this.$t('mine').确认删除当前收货地址吗,
				// 	confirmColor: '#ff0036',
				// 	success: async (res) => {
				// 		if (res.confirm) {

				// 		} else if (res.cancel) {
				// 			console.log('用户点击取消');
				// 		}
				// 	}
				// });
				const res = await this.$api.addressdelete({
					address_id: this.addressLists[this.seladdress].address_id
				})
				if (res && res.code == 1) {
					this.$utils.msg(this.$t('mine').地址删除成功)
					this.show = false
					this.addresslistAPI()
					// setTimeout(() => {
					// 	uni.navigateTo({
					// 		url: '/pages/address/newAddress'
					// 	})
					// }, 500)
				}
			},
			// 设置默认地址
			async setdefault(item) {
				let addObj = {
					real_name: item.real_name,
					area_code: item.area_code,
					phone: item.phone,
					province_id: item.province_id,
					detail: item.detail,
					is_default: item.is_default == 0 ? 1 : 0,
					address_id: item.address_id,
					city_id: item.city_id,
					district_id: item.district_id,
					street_id: item.street_id || ''
				}
				const res = await this.$api.addaddress(addObj)
				if (res && res.code == 1) {
					this.addresslistAPI()
				}
			}
		}
	}
</script>

<style>

</style>